<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <title>用户添加 - Nkuvr 后台管理系统</title>
    <meta name="author" content="yinqi">
    <link href="../../css/bootstrap.min.css" rel="stylesheet">
    <link href="../../css/materialdesignicons.min.css" rel="stylesheet">
    <link href="../../css/style.min.css" rel="stylesheet">
</head>

<body>
<div class="lyear-layout-web">
    <div class="lyear-layout-container">
        <!--左侧导航-->
        <aside th:replace="fragment/_fragment::navbar"></aside>


        <!--头部信息-->
        <header th:replace="fragment/_fragment::sidebar"></header>


        <!--页面主要内容-->
        <main class="lyear-layout-content">

            <div class="container-fluid">

                <div class="row">
                    <div class="col-lg-12">
                        <div class="card">
                            <div class="card-body">
                                <form action="#!" method="post" class="row">
                                    <div class="form-group col-md-12">
                                        <label for="studentNumber">学号</label>
                                        <input type="text" class="form-control" id="studentNumber"
                                               name="studentNumber"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="password">密码</label>
                                        <input type="password" class="form-control" id="password" name="password"/>
                                    </div>


                                    <div class="form-group col-md-12">
                                        <label for="realName">姓名</label>
                                        <input type="text" class="form-control" id="realName" name="realName"/>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="remark">状态</label>
                                        <div class="form-controls">
                                            <select name="remark" class="form-control" id="remark">
                                                <option value="0">管理员</option>
                                                <option value="1" selected>学生</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="gender">性别</label>
                                        <div class="clearfix">
                                            <label class="lyear-radio radio-inline radio-primary">
                                                <input type="radio" name="gender" value="0"><span>男</span>
                                            </label>
                                            <label class="lyear-radio radio-inline radio-primary">
                                                <input type="radio" name="gender" value="1"><span>女</span>
                                            </label>
                                        </div>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <label for="state">状态</label>
                                        <div class="form-controls">
                                            <select name="state" class="form-control" id="state">
                                                <option value="0" selected>启用</option>
                                                <option value="1">禁用</option>
                                            </select>
                                        </div>
                                    </div>

                                    <div class="form-group col-md-12">
                                        <a class="btn btn-primary ajax-post" target-form="add-form"
                                           href="javascript:void(0);" onclick="return doAdd();">确 定</a>
                                        <button type="button" class="btn btn-default"
                                                onclick="javascript:window.history.back(-1);return false;">返 回
                                        </button>
                                    </div>
                                </form>

                            </div>
                        </div>
                    </div>

                </div>

            </div>

        </main>

    </div>
</div>

<script type="text/javascript" src="../../js/jquery.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../js/perfect-scrollbar.min.js"></script>
<script type="text/javascript" src="../../js/main.min.js"></script>
<script src="../../layer/layer.js"></script>
<script type="text/javascript">
    function doAdd() {
        var password = $('#password').val();
        var studentNumber = $('#studentNumber').val();
        var realName = $('#realName').val();
        var gender = $('input:radio:checked').val();
        var stateObj = document.getElementById("state");
        var state = stateObj.selectedIndex;
        var remark = $('#remark').val();
        var loadingIndex = null;

        if (studentNumber === "") {
            layer.msg("学号不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        if (password === "") {
            layer.msg("密码不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        if (remark === "") {
            layer.msg("备注不能为空，请输入!", {time: 1000, icon: 5, shift: 6});
            return;
        }

        layer.confirm("确定新增该用户吗？", {btn: ['确认', '取消']}, function () {
            $.ajax({
                type: "post",
                url: "/user/doAdd",
                async: false,
                dataType: "json",
                data: {
                    "password": password,
                    "studentNumber": studentNumber,
                    "realName": realName,
                    "gender": gender,
                    "state": state,
                    "remark": remark
                },
                beforeSend: function () {
                    loadingIndex = layer.msg("处理中", {icon: 16});
                },
                success: function (result) {
                    layer.close(loadingIndex);
                    if (result.success) {
                        layer.msg("用户添加成功", {time: 1000, icon: 6});
                        window.location.href = "/user/list"
                    } else {
                        layer.msg("用户添加失败，请重新操作", {time: 1000, icon: 5, shift: 6});
                    }
                }
            });
            return false;
        });
    }
</script>
</body>
</html>